-- $Name: Terr Analog$
-- $Version: 0.1$
instead_version "1.3.1";

function init()
end;

pl = player {
	nam = 'Gamer';
	img = 'man.png';
	_vs = 6;
	_x = 4;
	_y = 4;
};

main = room {
	forcedsc = true;
	nam = 'Test';
	dsc = function()
		local outp = '';
		local x,y,n;
		local b = false;
		local view_y1 = pl._y - pl._vs;
		local view_y2 = pl._y + pl._vs;
		local view_x1 = pl._x - pl._vs;
		local view_x2 = pl._x + pl._vs;
		if pl._y - pl._vs < 1 then view_y1 = 1; view_y2 = 2 * pl._vs + 1; end;
		if pl._y + pl._vs > #field then view_y1 = #field - 2 * pl._vs; view_y2 = #field; end;
		if pl._x - pl._vs < 1 then view_x1 = 1; view_x2 = 2 * pl._vs + 1; end;
		if pl._x + pl._vs > #field[1] then view_x1 = #field[1] - 2 * pl._vs; view_x2 = #field; end;
		for y=view_y1, view_y2, 1 do
			for x=view_x1, view_x2, 1 do
				for n=1,#npc,1 do
					if y == npc[n]._y and x == npc[n]._x then outp = outp..img(field_par[field[y][x]].img..';'..npc[n].img); b = true; end;
				end;
				if b == false and y == pl._y and x == pl._x then outp = outp..img(field_par[field[y][x]].img..';man.png');	-- отрисовка игрока
				elseif b == false then outp = outp..img(field_par[field[y][x]].img);
				end;
				b = false;
			end;
			outp = outp..'^';
		end;
		return txtc(outp);
	end;
};

input.key = function(s, pr, key)
	if not pr or key == "escape"then return;
	elseif key == 'up' then move(0,-1);
	elseif key == 'down' then move(0,1);
	elseif key == 'left' then move(-1,0);
	elseif key == 'right' then move(1,0);
	end;
	return "look";
end;

function move(x,y)
	if pl._y+y < 1 or pl._y+y > #field or pl._x+x < 1 or pl._x+x > #field[1] then return;
	elseif field_par[field[pl._y+y][pl._x+x]].mov ~= 0 then pl._y = pl._y + y; pl._x = pl._x + x;
	end;
end;

global {
	npc = {
		[1] = {
			nam = 'NPC1';
			img = 'man.png';
			_x = 2;
			_y = 2;
		};
		[2] = {
			nam = 'NPC2';
			img = 'man.png';
			_x = 7;
			_y = 7;
		};
	};
}

global {
	field = {
		{'1','1','2','1','1','1','3','1','1','2','1','1','1','1','1'};
		{'1','1','2','1','1','1','3','1','1','2','1','1','1','1','1'};
		{'1','1','2','1','1','1','3','1','2','2','1','1','1','1','1'};
		{'1','1','2','1','1','1','3','1','2','1','1','1','1','1','1'};
		{'1','1','2','2','2','2','3','2','2','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
		{'1','1','1','1','1','1','3','1','1','1','1','1','1','1','1'};
	};
	field_par = {
		['1'] = {
			['nam'] = 'Трава';
			['mov'] = 1;
			['img'] = 'grass.png';
		};
		['2'] = {
			['nam'] = 'Стена';
			['mov'] = 0;
			['img'] = 'wall.png';
		};
		['3'] = {
			['nam'] = 'Дорога';
			['mov'] = 1;
			['img'] = 'road.png';
		};
	};
};
